package com.cjhstudy.mall.controller;

import com.cjhstudy.mall.consts.MallConst;
import com.cjhstudy.mall.form.CartAddForm;
import com.cjhstudy.mall.form.CartUpdateForm;
import com.cjhstudy.mall.pojo.User;
import com.cjhstudy.mall.service.ICartService;
import com.cjhstudy.mall.vo.CartVo;
import com.cjhstudy.mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author: phil
 * @ProjectName: mall
 * @Date: 2021/12/9
 */
@RestController
public class CartController {

    @Autowired
    private ICartService cartService;

    /**
     * 获取购物车的所有信息
     * @param session
     * @return
     */
    @GetMapping("/carts")
    public ResponseVo<CartVo> list(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.list(user.getId());
    }

    /**
     * @Valid ：表单验证前面必须加这个
     * @param cartAddForm 购物车添加
     * @return
     */
    @PostMapping("/carts")
    public ResponseVo<CartVo> add(@Valid @RequestBody CartAddForm cartAddForm,
                                  HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.add(user.getId(), cartAddForm);
    }


    /**
     * 更新购物车
     * @param productId 商品id
     * @param form 更新表单
     * @param session 登录用户的session
     * @return 返回更新后的list
     */
    @PutMapping("/carts/{productId}")
    public ResponseVo<CartVo> update(@PathVariable Integer productId,
                                     @Valid @RequestBody CartUpdateForm form,
                                     HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.update(user.getId(), productId,form);
    }

    /**
     * 删除购物车的商品
     * @param productId 商品id
     * @param session 用户登录的session
     * @return 返回删除后的商品列表
     */
    @DeleteMapping("/carts/{productId}")
    public ResponseVo<CartVo> delete(@PathVariable Integer productId,
                                     HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.delete(user.getId(), productId);
    }

    /**
     * 全选购物车里的所有列表信息
     * @param session 用户登录的session
     * @return 返回购物车的所有全部选取的信息
     */
    @PutMapping("/carts/selectAll")
    public ResponseVo<CartVo> selectAll(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.selectAll(user.getId());
    }


    /**
     * 全不选购物车里的所有信息
     * @param session 用户登录的session
     * @return 返回全不选的购物车信息
     */
    @PutMapping("/carts/unSelectAll")
    public ResponseVo<CartVo> unSelectAll(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.unSelectAll(user.getId());
    }


    /**
     * 查询购物车的所有的商品数量
     * @param session 用户登录的session
     * @return 返回购物车的所有的商品数量
     */
    @GetMapping("/carts/products/sum")
    public ResponseVo<Integer> sum(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.sum(user.getId());
    }

}
